'use strict'

angular.module('mainApp')
.controller('submitOrderCtrl', function($scope, $state, $stateParams, tjzHttp, constant, ngDialog){
    var jobId = $stateParams.jobId;
    var init = function(){
        tjzHttp.postRequest(constant.url+ '/jobinfo',{jobId:jobId}).then(function(res){
            if(res.data.code == 0){
                $scope.info = res.data.data;
                if($scope.info.imageUrl){
                    var tmp = $scope.info.imageUrl.replace('127.0.0.1:8080/TJZ',constant.url);
                    $scope.info.imageUrl = tmp;
                }
            }
        });
    }
    $scope.buy = function(){
        tjzHttp.postRequest(constant.url + '/createorder', {jobId:jobId}).then(function(res){
            if(res.data.code == 0){
                var id = res.data.data.id;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', function ($scope) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '订单创建成功';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                }).closePromise.then(function(){
                    $state.go('pay',{id:id,jobId:jobId});
                });
            }else{
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope', 'ngDialog', function ($scope, ngDialog) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = $scope.ngDialogData.message;
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                });
            }
        })
    }
    init();
})
.controller('payCtrl', function($scope, $state, $stateParams, tjzHttp, constant, ngDialog){
    var jobId = $stateParams.jobId;
    var id = $stateParams.id;
    var init = function(){
        tjzHttp.postRequest(constant.url+ '/jobinfo',{jobId:jobId}).then(function(res){
            if(res.data.code == 0){
                $scope.info = res.data.data;
                if($scope.info.imageUrl){
                    var tmp = $scope.info.imageUrl.replace('127.0.0.1:8080/TJZ',constant.url);
                    $scope.info.imageUrl = tmp;
                }
            }
        });
    }
    $scope.pay = function(){
        tjzHttp.postRequest(constant.url + '/pay', {id:id,password:$scope.password}).then(function(res){
            if(res.data.code == 0){
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', 'ngDialog', function ($scope, ngDialog) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '恭喜您，支付成功';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                }).closePromise.then(function () {
                    $state.go('index');
                });
            }else{
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope', 'ngDialog', function ($scope, ngDialog) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = $scope.ngDialogData.message;
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                });
            }
        })
    }
    init();
})
.controller('orderDetailCtrl', function ($scope, $state, $stateParams, tjzHttp, constant, ngDialog) {
    var jobId = $stateParams.jobId;
    var id = $stateParams.id;
    var init = function(){
        tjzHttp.postRequest(constant.url+ '/jobinfo',{jobId:jobId}).then(function(res){
            if(res.data.code == 0){
                $scope.info = res.data.data;
                if($scope.info.imageUrl){
                    var tmp = $scope.info.imageUrl.replace('127.0.0.1:8080/TJZ',constant.url);
                    $scope.info.imageUrl = tmp;
                }
                tjzHttp.postRequest(constant.url+ '/getOrderInfo',{id:id}).then(function(res){
                    if(res.data.code == 0){
                        $scope.info.date = res.data.list[0].date.split('.0')[0];
                    }
                });
            }
        });

    }
    $scope.finishOrder = function(){
        tjzHttp.postRequest(constant.url + '/finished', {id:id}).then(function(res){
            if(res.data.code == 0){
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', function ($scope) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '恭喜您，成功完成订单';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                }).closePromise.then(function () {
                    $state.go('evaluate',{jobId:jobId,id:id});
                });
            }else{
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope', 'ngDialog', function ($scope, ngDialog) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = $scope.ngDialogData.message;
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                })
            }
        })
    }
    init();
})
.controller('evaluateCtrl', function($scope, $state, $stateParams, tjzHttp, constant, ngDialog){
    var id = $stateParams.id;
    var jobId = $stateParams.jobId;
    $scope.orderId = id;
    var init = function(){
        tjzHttp.postRequest(constant.url+ '/getOrderInfo',{id:id}).then(function(res){
            if(res.data.code == 0){
                $scope.date = res.data.list[0].date.split('.0')[0];
            }
        });
    }
    $scope.save = function(){
        var data = {
            jobId: jobId,
            score: $scope.score,
            evaluates: $scope.evaluates
        }
        tjzHttp.postRequest(constant.url + '/createJobEvaluates', data).then(function(res){
            if(res.data.code == 0){
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope', function ($scope) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '恭喜您，评价成功';
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                })
            }else{
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope', 'ngDialog', function ($scope, ngDialog) {
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = $scope.ngDialogData.message;
                        $scope.yes = function () {
                            $scope.closeThisDialog();
                        }
                    }]
                })
            }
        })
    }
    init();
})